<?php

namespace App\model;

use Illuminate\Database\Eloquent\Model;
use Composer\Cache;
use App\BuildMatch;
use App\Website;

/**
 * App\model\Building
 *
 * @property-read \App\AreaBuild $areaBuild
 * @property-read \App\Broker $broker
 * @property-read mixed $build_match
 * @property-read mixed $slider
 * @property-read mixed $tag
 * @property-read \App\model\Park $park
 * @mixin \Eloquent
 * @property int $id
 * @property string|null $area 面积
 * @property int|null $area_build_id 面积区域id
 * @property int|null $rent 租金
 * @property string|null $address 地址
 * @property int|null $park_id 园区id
 * @property string|null $is_company 是否注册公司：0否1是
 * @property string|null $use_rate 使用率
 * @property string|null $is_separate 是否分割：0否1是
 * @property string|null $renovation 装修：0为毛坯1为精装修
 * @property string|null $floor 所在楼层
 * @property string|null $build_match_id 配套
 * @property string|null $contact 联系人
 * @property string|null $telephone 联系电话
 * @property string|null $is_owner 是否业主：0否1是
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $code 编码
 * @property string|null $standard_height 标准层高
 * @property int|null $manage_cost 管理费用
 * @property string|null $is_recommend 是否推荐
 * @property string|null $title 标题
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereAreaBuildId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereBuildMatchId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereContact($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereFloor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereIsCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereIsOwner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereIsSeparate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereManageCost($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereParkId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereRenovation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereStandardHeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereUseRate($value)
 * @property string|null $p_district_id
 * @property string|null $p_city_id
 * @property string|null $p_province_id
 * @property string|null $logo 首图
 * @property string|null $latitude 经度
 * @property string|null $longitude 纬度
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building wherePProvinceId($value)
 * @property string|null $desc
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Building whereDesc($value)
 */
class Building extends Model
{
    //
    //
    protected $table="building";
    protected $appends=['build_match'];
    public function getLogoAttribute($value)
    {
        if($value){
            if(strstr($value,'http')){
                return $value.'?imageView2/0/w/512/h/384';
            }else{
                return Storage::url($value).'?imageView2/0/w/512/h/384';
            }
        }
        $silder= explode(',',$this->attributes['slider']);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(!preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                return   $value.'?imageView2/0/w/512/h/384';
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
    }
    public function getTagAttribute($value){
        return explode(',',$value);
    }
    public function getSliderAttribute($value){
        $silder= explode(',',$value);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                $map['video'][]= $value;
            }else{
                $webSite = Website::find(1);
                $url = config('admin.app_outurl1').'/'.$webSite->watermark.'?imageView2/2/w/360/h/250';
                $map['image'][]=$v.'?imageView2/2/w/960/h/720|watermark/1/image/'.\Qiniu\base64_urlSafeEncode($url).'/gravity/'.$webSite->watermark_position;
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
        return $map;
    }

    public function getBuildMatchAttribute()
    {
        $standard = \Illuminate\Support\Facades\Cache::remember('build_match', '3', function () {
            return BuildMatch::pluck('name', 'id')->toArray();
        });
        $option = $this->attributes['build_match_id'];

        $array = [];
        $option = explode(',', $option);
        foreach ($option as $v) {
            if (isset($standard[$v])) {
                $array[] = $standard[$v];
            }
        }
        return $array;
    }
    /**
     * 获取匹配对象
     * @return BuildMatch[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getBuildMatchIdAttribute()
    {
        $option = $this->attributes['build_match_id'];
        $option = explode(',', $option);
        return BuildMatch::whereIn('id',$option)->get();
    }

    public function park(){
        return $this->belongsTo(\App\model\Park::class,'park_id');
    }

    public function areaBuild(){
        return $this->belongsTo(\App\AreaBuild::class,'area_build_id');
    }

    public function broker(){
        return $this->belongsTo(\App\model\Broker::class,'contact');
    }
}
